package com.gqj.meter.service.impl;

import java.util.List;
import java.util.Map;

import com.gqj.common.constant.UserConstants;
import com.gqj.common.exception.ServiceException;
import com.gqj.common.utils.StringUtils;
import com.gqj.common.utils.bean.BeanValidators;
import com.gqj.meter.domain.MdIt;
import com.gqj.meter.domain.MdMeter;
import com.gqj.meter.mapper.MdItMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gqj.meter.mapper.MdConcentratorMapper;
import com.gqj.meter.domain.MdConcentrator;
import com.gqj.meter.service.IMdConcentratorService;

import javax.validation.Validator;

/**
 * 集中器Service业务层处理
 *
 * @author lxq
 * @date lxq-02-10
 */
@Service
public class MdConcentratorServiceImpl implements IMdConcentratorService
{
    @Autowired
    private MdConcentratorMapper mdConcentratorMapper;
    private static final Logger log = LoggerFactory.getLogger(MdConcentratorServiceImpl.class);
    @Autowired
    protected Validator validator;

    /**
     * 查询集中器
     *
     * @param concentratorId 集中器主键
     * @return 采集器
     */
    @Override
    public MdConcentrator selectMdConcentratorByConcentratorId(Long concentratorId)
    {
        return mdConcentratorMapper.selectMdConcentratorByConcentratorId(concentratorId);
    }

    /**
     * 查询集中器列表
     *
     * @param mdConcentrator 采集器
     * @return 采集器
     */
    @Override
    public List<MdConcentrator> selectMdConcentratorList(MdConcentrator mdConcentrator)
    {
        return mdConcentratorMapper.selectMdConcentratorList(mdConcentrator);
    }

    /**
     * 新增集中器
     *
     * @param mdConcentrator 采集器
     * @return 结果
     */
    @Override
    public int insertMdConcentrator(MdConcentrator mdConcentrator)
    {
        return mdConcentratorMapper.insertMdConcentrator(mdConcentrator);
    }

    /**
     * 修改集中器
     *
     * @param mdConcentrator 采集器
     * @return 结果
     */
    @Override
    public int updateMdConcentrator(MdConcentrator mdConcentrator)
    {
        mdConcentrator.setMeterNo(mdConcentrator.getRfidNo());
        return mdConcentratorMapper.updateMdConcentrator(mdConcentrator);
    }

    /**
     * 批量删除集中器
     *
     * @param concentratorIds 需要删除的集中器主键
     * @return 结果
     */
    @Override
    public int deleteMdConcentratorByConcentratorIds(Long[] concentratorIds)
    {
        return mdConcentratorMapper.deleteMdConcentratorByConcentratorIds(concentratorIds);
    }

    /**
     * 删除集中器信息
     *
     * @param concentratorId 集中器主键
     * @return 结果
     */
    @Override
    public int deleteMdConcentratorByConcentratorId(Long concentratorId)
    {
        return mdConcentratorMapper.deleteMdConcentratorByConcentratorId(concentratorId);
    }

    /**
     * 校验资产编号码是否唯一
     *
     * @param mdConcentrator 采集器
     * @return 结果
     */
    public String checkMeterNoUnique(MdConcentrator mdConcentrator){
        Long concentratorId = StringUtils.isNull(mdConcentrator.getConcentratorId()) ? -1L : mdConcentrator.getConcentratorId();
        MdConcentrator info = mdConcentratorMapper.checkMeterNoUnique(mdConcentrator.getMeterNo());
        if (StringUtils.isNotNull(info) && info.getConcentratorId().longValue() != concentratorId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验rfid编号/条形码是否唯一
     *
     * @param mdConcentrator 采集器
     * @return 结果
     */
    public String checkRfidNoUnique(MdConcentrator mdConcentrator){
        Long concentratorId = StringUtils.isNull(mdConcentrator.getConcentratorId()) ? -1L : mdConcentrator.getConcentratorId();
        MdConcentrator info = mdConcentratorMapper.checkRfidNoUnique(mdConcentrator.getRfidNo());
        if (StringUtils.isNotNull(info) && info.getConcentratorId().longValue() != concentratorId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 导入集中器数据
     *
     * @param concentratorList 集中器列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    public String importConcentrator(List<MdConcentrator> concentratorList, Boolean isUpdateSupport, String operName){
        if (StringUtils.isNull(concentratorList) || concentratorList.size() == 0) {
            throw new ServiceException("导入集中器不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (MdConcentrator concentrator : concentratorList) {
            try {
                BeanValidators.validateWithException(validator, concentrator);
                // 验证是否存在这个数据
                MdConcentrator m = mdConcentratorMapper.selectMdConcentratorByRfidNo(concentrator.getRfidNo());
                if (StringUtils.isNull(m)) {
                    concentrator.setServiceStatus("00");
                    this.insertMdConcentrator(concentrator);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、采集器 " + concentrator.getRfidNo() + " 导入成功");
                } else if (isUpdateSupport) {
                    concentrator.setConcentratorId(m.getConcentratorId());
                    this.updateMdConcentrator(concentrator);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、采集器 " + concentrator.getRfidNo() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、采集器 " + concentrator.getRfidNo() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、采集器 " + concentrator.getRfidNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    //查询业务状态数量
    @Override
    public int[] getServiceStatusNum() {
        Map<String, Object> map = mdConcentratorMapper.getServiceStatusNum();

        int[] counts = new int[6];
        counts[0] = ((Long) map.get("count_00")).intValue();
        counts[1] = ((Long) map.get("count_01")).intValue();
        counts[2] = ((Long) map.get("count_02")).intValue();
        counts[3] = ((Long) map.get("count_03")).intValue();
        counts[4] = ((Long) map.get("count_04")).intValue();
        counts[5] = ((Long) map.get("count_05")).intValue();
        return counts;
    }

    /**
     * 通过rfidCode查询采集器信息
     *
     * @param rfidCodes 标签号
     * @return 采集器
     */
    @Override
    public List<MdConcentrator> selectMdConcentratorByRfidBath(String[] rfidCodes){
        return mdConcentratorMapper.selectMdConcentratorByRfidBath(rfidCodes);
    }

}
